package com.leonardo.persistenceme.metadatahandlers.taghandlers;

import org.xml.sax.Attributes;

import com.leonardo.persistenceme.exceptions.XmlReadingException;
import com.leonardo.persistenceme.metadatahandlers.PersistenceMetadataContainer;

public class BeanTagHandler extends TagHandler {

	public String getTagName() {
		return "bean";
	}

	public void startTag(Attributes attributes){ 
		if(owner.isTagOpen("bean"))
			throw new XmlReadingException("Incorrect place of tag bean");

		int idIndex = attributes.getIndex("id"); 
		int typeIndex = attributes.getIndex("type");
		
		if(idIndex < 0)
			throw new XmlReadingException("Bean without id found");
		
		String id = attributes.getValue(idIndex) ;
		String type = typeIndex < 0 ? PersistenceMetadataContainer.PERSISTENT :
				attributes.getValue(typeIndex);
		
		owner.newBean(id);
		owner.setType(type);
	}
	
	public void endTag() { 
		owner.addCurrentBean();
	}
	
}
